package com.blue.jdbc.dialect;

import com.blue.jdbc.annotation.LockModeType;

/**
 * MySQL 数据库方言
 * 
 * @author zhengj
 * @since 1.0 2011-4-2
 */
public class MySQLDialect implements IDialect
{
	private static final String QUOT = "`";
	
	public MySQLDialect()
	{
	}

	@Override
	public String escape()
	{
		return QUOT;
	}

	@Override
	public String escape(String key)
	{
		return QUOT + key + QUOT;
	}

	@Override
	public String page(String sql, int start, int size)
	{
		StringBuilder t = new StringBuilder(sql);
		t.append(" limit ")
			.append(start)
			.append(",")
			.append(size);
		return t.toString();
	}

	@Override
	public String lock(String sql, LockModeType type)
	{
		if (type == LockModeType.NONE)
			return sql;
		
		StringBuilder t = new StringBuilder(sql);
		if (type == LockModeType.WRITE)
			t.append(" for update");
		
		return t.toString();
	}
}
